package com.android.questions.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.base.util.DisplayUtil;
import com.android.base.util.SystemUtil;
import com.android.base.util.ToastUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.databinding.DialogJointQqBinding;
import com.hx.currency.HXCurrencySDK;
import com.hx.currency.data.api.BindResp;
import com.qq.e.o.data.HttpUtil;
import com.qq.e.o.utils.JsonUtil;

public class DialogJointQQ extends Dialog implements View.OnClickListener {

    private DialogJointQqBinding jointQQBinding;
    private final Activity activity;
    private OnClickLister clickLister;

    public DialogJointQQ(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        jointQQBinding = DialogJointQqBinding.inflate(getLayoutInflater());
        setContentView(jointQQBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
    }

    private void initView() {
        ActivityClickUtil.getInstance().clear();

        jointQQBinding.ivJoinBind.setOnClickListener(this);
        jointQQBinding.ivClose.setOnClickListener(this);
    }

    private void bindAndJoinQQ() {
        String qq = jointQQBinding.etQq.getText().toString().trim();
        if (TextUtils.isEmpty(qq)) {
            ToastUtil.showShortToast(activity, "QQ号不能为空");
            return;
        }

        if (qq.length() < 5) {
            ToastUtil.showShortToast(activity, "QQ号错误，请重新输入");
            return;
        }

        HXCurrencySDK.getInstance().bind(4, "", "", qq, 0, new HttpUtil.HttpUtilCallback() {
            @Override
            public void onSuccess(int reqCode, String response) {
                DQLog.e("绑定QQ成功：" + response);
                BindResp bindResp = JsonUtil.parseObject(response, BindResp.class);
                if (bindResp != null) {
                    int errorCode = bindResp.getErrorCode();
                    String errorMessage = bindResp.getErrorMessage();
                    if (errorCode == 0) {
                        AppConfig.hasQQ = 1;
                        ToastUtil.showShortToast(activity, "绑定QQ成功");
                        SystemUtil.joinQQGroup(activity, AppConfig.QQ_GROUP_KEY);
                        dismiss();
                    } else {
                        ToastUtil.showShortToast(activity, errorMessage);
                    }
                } else {
                    ToastUtil.showShortToast(activity, "绑定QQ失败");
                }
            }

            @Override
            public void onFailed(int reqCode, Throwable throwable) {
                DQLog.e("绑定微信失败：" + throwable.getMessage());
                ToastUtil.showShortToast(activity, "绑定QQ失败：" + throwable.getMessage());
            }
        });
    }

    public DialogJointQQ setClickLister(OnClickLister onClickLister) {
        this.clickLister = onClickLister;
        return this;
    }

    @Override
    public void onClick(View view) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (view.getId() == R.id.iv_join_bind) {
            bindAndJoinQQ();
        } else if (view.getId() == R.id.iv_close) {
            dismiss();
        }
    }

    public interface OnClickLister {
        void onCancel();
    }
}
